package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX;
import com.augurit.gzcgwsp.ywcmn.dao.YwComSjclDao;
import com.augurit.gzcgwsp.ywcmn.service.IYwComYslService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComYslForm;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;

@Service
@Transactional
public class YwComYslServiceImpl implements IYwComYslService {
    @Autowired
    private YwComSjclDao entityDao;
    @Autowired
    private IWfBusTemplateService wfBusTemplateService;
    @Autowired
    private IWfBusTemplateServiceEX wfBusTemplateServiceEX;

    public Page<YwComYslForm> getYslDataList(Page<YwComYslForm> page, YwComYslForm form,LoginUserForm userForm) {
        
        // 建立新的分页对象
        Page pg = PageUtils.newPageInstance(page);
        
        //-----------------
//    	TODO 燃气收案只看到类型为燃气的，以此类推
    	Long[] roleIds = userForm.getRoleIds();
    	boolean ifHasPermission = false;//是否有收案权限（目前只有窗口人员有收案权限）
    	//获取角色配置
    	Map<String,String> roleIdsMap = wfBusTemplateServiceEX.getRoleIds();
    	boolean sRqc = false;//是否有   市燃气处   收案权限
    	boolean qRqc = false;//是否有   区燃气处   收案权限
    	
    	boolean sYnc = false;//是否有   市余泥处   收案权限
    	boolean qYnc = false;//是否有   区余泥处   收案权限
    	
    	boolean sSrc = false;//是否有   市市容处   收案权限
    	boolean qSrc = false;//是否有   区市容处   收案权限
    	
    	boolean sJgc = false;//是否有   市景观处   收案权限
    	boolean qJgc = false;//是否有   区景观处   收案权限
    	
    	boolean sSsc = false;//是否有   市设施处   收案权限
    	boolean qSsc = false;//是否有   区设施处   收案权限
    	
    	boolean qHwcLy = false;//是否有   区环卫处   收案权限
    	boolean qHwcSy = false;//是否有   区环卫处   收案权限
    	boolean qHwcFqw = false;//是否有   区环卫处   收案权限
    	
    	boolean flag = false;//是否已经有至少一个权限的标志，用于判定sql是用and还是用or
    	
    	if(roleIds != null && roleIds.length > 0){
    		for(int i=0;i<roleIds.length;i++){
    			if(roleIdsMap.get("rqcIds").contains(roleIds[i].toString())){//燃气处
    				sRqc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("quRqcIds").contains(roleIds[i].toString())){//区燃气处
    				qRqc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("yncIds").contains(roleIds[i].toString())){//余泥处
    				sYnc = true;
    				ifHasPermission = true;
    			} 
    			if(roleIdsMap.get("quyncIds").contains(roleIds[i].toString())){//区余泥处
    				qYnc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("jgcIds").contains(roleIds[i].toString())){//景观处
    				sJgc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("srcIds").contains(roleIds[i].toString())){//市容处
    				sSrc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("quSrcIds").contains(roleIds[i].toString())){//区市容处
    				qSrc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("sscIds").contains(roleIds[i].toString())){//设施处
    				sSsc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("hwcLyIds").contains(roleIds[i].toString())){//环卫处-陆域
    				qHwcLy = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("hwcSyIds").contains(roleIds[i].toString())){//环卫处-水域
    				qHwcSy = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("hwcFqwIds").contains(roleIds[i].toString())){//环卫处-废弃物中心
    				qHwcFqw = true;
    				ifHasPermission = true;
    			}
    		}    		
    	}
        //-----------------
        
        StringBuffer sql = new StringBuffer();
        sql.append("select t.* ");
        sql.append("  from (" );
        
        if(sRqc == true){//有燃气处市的收案权限
			if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq , t.cbyw branch, 1 optid from Yw_Rq_Ssgd_Sqb t WHERE t.ysl = " + form.getYsl()+" and t.branch='rqssgd'");
			sql.append("        UNION ALL ");
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 2 optid from Yw_Rq_Qyzz_Info t WHERE t.ysl = " + form.getYsl()+" and t.branch='rqrsqjazwxqyzz'");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 3 optid from YW_RQ_QYJYXK t WHERE t.ysl = " + form.getYsl() + " and t.branch='rqjyxkz_gdqy' ");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 4 optid from YW_RQ_QYJYXK t WHERE t.ysl = " + form.getYsl() + " and t.branch='rqjyxkz_pzqy' ");
    	}
        
        if(qRqc == true){//有燃气处区的收案权限
        	if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq , t.cbyw branch, 111 optid from Yw_Rq_Ssgd_Sqb t WHERE t.ysl = " + form.getYsl()+" and t.branch='rqssgdqx'");
			sql.append("        UNION ALL ");
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 222 optid from Yw_Rq_Qyzz_Info t WHERE t.ysl = " + form.getYsl()+" and t.branch='rqrsqjazwxqyzzqx'");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 333 optid from YW_RQ_QYJYXK t WHERE t.ysl = " + form.getYsl() + " and t.branch='rqjyxkzqx_gdqy' ");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 444 optid from YW_RQ_QYJYXK t WHERE t.ysl = " + form.getYsl() + " and t.branch='rqjyxkzqx_pzqy' ");
    	}
        
        if(sYnc == true){//有余泥处市的权限
			if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.branch branch, 12 optid from YW_YN_JZFQWCZC_JBXX t WHERE t.ysl = " + form.getYsl() + " and t.branch like 'jzljczz\\_%' escape '\\' ");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.branch branch, 13 optid from YW_YN_JZFQWCZC_PFSN t WHERE t.ysl = " + form.getYsl() + " and t.branch like 'jzfqwczzpf\\_%' escape '\\' ");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.branch branch, 14 optid from YW_YN_JZFQWCZC_XN t WHERE t.ysl = " + form.getYsl() + " and t.branch like 'jzfqwczzxn\\_%' escape '\\' ");
    	}
        
        if(qYnc == true){//有余泥处区的权限
			if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.branch branch, 15 optid from YW_YN_JZFQWCZC_JBXX t WHERE t.ysl = " + form.getYsl() + " and t.branch like 'jzljczzqx\\_%' escape '\\' ");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.branch branch, 16 optid from YW_YN_JZFQWCZC_PFSN t WHERE t.ysl = " + form.getYsl() + " and t.branch like 'jzfqwczzpfqx\\_%' escape '\\' ");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.branch branch, 17 optid from YW_YN_JZFQWCZC_XN t WHERE t.ysl = " + form.getYsl() + " and t.branch like 'jzfqwczzxnqx\\_%' escape '\\' ");
    	}
        
        if(sJgc == true){//有景观处市的权限
			if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 5 optid from YW_JG_HWGG_SQB t WHERE t.ysl = " + form.getYsl());
    	}
        
        if(sSrc == true){//有市容处市的权限
			if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.tbrq sqrq, t.jarq ,t.cbyw branch, 6 optid from SR_DLZYSQB_V1 t WHERE t.ysl = " + form.getYsl()+" and t.branch='csdllszysp'");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.jsdw topic, t.cbyw cbyw, t.tbrq sqrq, t.jarq ,t.cbyw branch, 7 optid from SR_DLWJSQB_V4 t WHERE t.ysl = " + form.getYsl()+" and t.branch='csdlwj'");
    	}
        
        if(qSrc == true){//有市容处区的权限
			if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.sqdw topic, t.cbyw cbyw, t.tbrq sqrq, t.jarq ,t.cbyw branch, 666 optid from SR_DLZYSQB_V1 t WHERE t.ysl = " + form.getYsl()+" and t.branch='csdllszyspqx'");
	        sql.append("        UNION ALL ");
	        sql.append("        select t.id, t.ysl_no yslNo, t.jsdw topic, t.cbyw cbyw, t.tbrq sqrq, t.jarq ,t.cbyw branch, 777 optid from SR_DLWJSQB_V4 t WHERE t.ysl = " + form.getYsl()+" and t.branch='csdlwjqx'");
    	}
        
        if(sSsc == true){//有设施处市的权限
			if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
			sql.append("        select t.id, t.ysl_no yslNo, t.gcmc topic, t.cbyw cbyw, t.sarq sqrq, t.jarq ,t.cbyw branch, 11 optid from SS_CCGBSQB_V1 t WHERE t.ysl = " + form.getYsl());
    	}
        
        if(qHwcLy == true){//有区环卫处陆域的权限
        	if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
        	sql.append("        select t.id, t.ysl_no yslNo, t.qymc topic, t.cbyw cbyw, t.tbrq sqrq, t.jarq ,t.cbyw branch, 10 optid from HW_SHLJQS_V1 t WHERE t.ysl = " + form.getYsl());
    	}
        
        if(qHwcSy == true){//有区环卫处水域的权限
        	if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
        	sql.append("        select t.id, t.ysl_no yslNo, t.qymc topic, t.cbyw cbyw, t.slrq sqrq, t.jarq ,t.cbyw branch, 9 optid from SYSHLJ_V1 t WHERE t.ysl = " + form.getYsl());
        }
        
        if(qHwcFqw == true){//有区环卫处废弃物的权限
        	if(sql.toString().contains("UNION")){
				sql.append("        UNION ALL ");
			}
        	sql.append("        select t.id, t.ysl_no yslNo, t.qymc topic, t.cbyw cbyw, t.sqrq sqrq, t.jarq ,t.cbyw branch, 8 optid from SHLJ_CZ_V1 t WHERE t.ysl = " + form.getYsl());
        }            
        
        sql.append(") t  where 1=1 ");
        
        // 查询条件
        if(form != null){
/*            if(form.getAcceptNo() != null && form.getAcceptNo().trim().length() > 0){
                sql.append(" and t.acceptNo like '%" + form.getAcceptNo() + "%'");
            }*/
            if(form.getTopic() != null && form.getTopic().trim().length() > 0){
                sql.append(" and t.topic like '%" + form.getTopic() + "%'");
            }
            if (form.getOptid() != null && form.getOptid().longValue() != 0) {
                sql.append(" and t.optid = " + form.getOptid());
            }
        }
         sql.append(" order by t.sqrq desc ");

        if (pg.isAutoCount()) {
             BigDecimal totalCount = (BigDecimal) entityDao.getSession().createSQLQuery("select count(*) from (" + sql + ")").uniqueResult();
             pg.setTotalCount(totalCount.longValue());
        }
        SQLQuery q = entityDao.getSession().createSQLQuery(sql.toString());
        q.setFirstResult(pg.getFirst() - 1);
        q.setMaxResults(pg.getPageSize());
        
        List<WfBusTemplateForm> tplList = wfBusTemplateService.getAllActive();
        List<YwComYslForm> formList = new ArrayList<YwComYslForm>();
        
        List<Map<String, Object>> resultList = q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
        for (Map<String, Object> dataMap : resultList) {	
            Long id = ((BigDecimal) dataMap.get("ID")).longValue();
            String yslNo = (String) dataMap.get("YSLNO");
            String topic = (String) dataMap.get("TOPIC");
            Date sqrq = (Date) dataMap.get("SQRQ");
            Date jarq = (Date) dataMap.get("JARQ");
            Long optid = ((BigDecimal) dataMap.get("OPTID")).longValue();
            String cbyw = (String) dataMap.get("CBYW");
            
            YwComYslForm rowForm = new YwComYslForm();
            rowForm.setId(id);
           // rowForm.setAcceptNo(acceptNo);
            rowForm.setYslNo(yslNo);
            rowForm.setTopic(topic);
            rowForm.setSqrq(DateUtils.dateTimeToString(sqrq, DateUtils.YEAR_MOUTH_DAY_));
            rowForm.setJarq(DateUtils.dateTimeToString(jarq, DateUtils.YEAR_MOUTH_DAY_));
            rowForm.setOptid(optid);
            if (optid.longValue() == 1) {
            	rowForm.setTemplateCode("rqssgd");
            }else if (optid.longValue() == 111) {
            	rowForm.setTemplateCode("rqssgdqx");
            }else if (optid.longValue() == 2) {
            	rowForm.setTemplateCode("rqrsqjazwxqyzz");
            }else if (optid.longValue() == 222) {
            	rowForm.setTemplateCode("rqrsqjazwxqyzzqx");
            }else if (optid.longValue() == 3) {
            	rowForm.setTemplateCode("rqjyxkz_gdqy");
            }else if (optid.longValue() == 333) {
            	rowForm.setTemplateCode("rqjyxkzqx_gdqy");
            }else if (optid.longValue() == 4) {
            	rowForm.setTemplateCode("rqjyxkz_pzqy");
            }else if (optid.longValue() == 444) {
            	rowForm.setTemplateCode("rqjyxkzqx_pzqy");
            }else if (optid.longValue() == 5) {
            	rowForm.setTemplateCode("hwggshsqb");
			}else if (optid.longValue() == 6) {
            	rowForm.setTemplateCode("csdllszysp");
			}else if (optid.longValue() == 666) {
            	rowForm.setTemplateCode("csdllszyspqx");
			}else if (optid.longValue() == 7) {
            	rowForm.setTemplateCode("csdlwj");
			}else if (optid.longValue() == 777) {
            	rowForm.setTemplateCode("csdlwjqx");
			}else if (optid.longValue() == 8) {
            	rowForm.setTemplateCode("shljcz");
			}else if (optid.longValue() == 9) {
            	rowForm.setTemplateCode("shljys");
			}else if (optid.longValue() == 10) {
            	rowForm.setTemplateCode("shljqssj");
			}else if (optid.longValue() == 11) {
            	rowForm.setTemplateCode("ccgbggcs");
			}else if (optid.longValue() == 12 || optid.longValue() == 13 || optid.longValue() == 14 ||
					optid.longValue() == 15 || optid.longValue() == 16 || optid.longValue() == 17) {
				String templateCode = (String) dataMap.get("BRANCH");
            	rowForm.setTemplateCode(templateCode);
			}
            
            for (WfBusTemplateForm tplForm : tplList) {
                if (tplForm.getTemplateCode().equals(rowForm.getTemplateCode())) {
                    rowForm.setCbyw(tplForm.getTemplateName());
                }
            }
            formList.add(rowForm);
        }
        PageUtils.copy(pg, formList, page);
        return page;
    }

    public Page<YwComYslForm> search(Page<YwComYslForm> page,
            List<PropertyFilter> filters) {
        // TODO Auto-generated method stub
        return null;
    }

    public Page<YwComYslForm> search(Page<YwComYslForm> page,
            YwComYslForm form) {
        // TODO Auto-generated method stub
        return null;
    }

    public void delete(Long... ids) {
        // TODO Auto-generated method stub

    }

    public YwComYslForm get(Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    public void save(YwComYslForm... forms) {
        // TODO Auto-generated method stub

    }

}
